Skip to content

Conversation

@chrispader
Copy link
Member

@chrispader chrispader commented Nov 3, 2025

Changes the current JS lock implementation in favor of a DatabaseQueue, which is used for both transactions and batch executions


Note

Replace JS lock mechanism with a DatabaseQueue that serializes transactions and executeBatch operations, integrate it with session lifecycle, and update tests accordingly.

  • Core:
    • DatabaseQueue: Add package/src/DatabaseQueue.ts with openDatabaseQueue/closeDatabaseQueue, queueOperationAsync, startOperationSync, and per-DB queue/inProgress tracking.
    • Transactions: Refactor transaction to run via DatabaseQueue, add finalized-state guards, wrap errors with NitroSQLiteError, support optional exclusive begin, and return generic Result.
    • Batch Ops: Route executeBatch/executeBatchAsync through DatabaseQueue; enforce throwIfDatabaseIsNotOpen.
    • Session Lifecycle: Wire open/close to openDatabaseQueue/closeDatabaseQueue; remove legacy lock usage.
    • API/Exports: Re-export NitroSQLiteError; update NitroSQLiteConnection.transaction signature to be generic and return the callback result.
  • Tests:
    • Add example/src/tests/unit/specs/DatabaseQueue.spec.ts to verify queuing and error propagation for mixed operations.
    • Update unit tests to use isNitroSQLiteError, adjust paths, and rely on resetTestDb(); expose testDbQueue/TEST_DB_NAME for assertions.
    • Include ../package in example/tsconfig.json.

Written by Cursor Bugbot for commit 9bcae9c. This will update automatically on new commits. Configure here.

@chrispader chrispader marked this pull request as ready for review November 3, 2025 19:24
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@chrispader chrispader changed the title feat: Add locks for transactions and improve errors feat: Migrate JS-sided locks to DatabaseQueue for transactions and executeBatch commands Nov 4, 2025
openDatabaseQueue(options.name)
} catch (error) {
throw NitroSQLiteError.fromError(error)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Resource leak in database open error handling

Resource leak in error handling: If HybridNitroSQLite.open() succeeds but openDatabaseQueue() throws an error (e.g., if the database queue is already open), the native database will remain open while the database queue is not created. This leaves the system in an inconsistent state where the native database is open but the queue tracking doesn't exist. The code should either close the native database in the error handler, or call openDatabaseQueue() before opening the native database.

Fix in Cursor Fix in Web

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true, but has been the behavior previously as well. I will address having multiple simultaneous database connections in a follow-up PR

} catch (error) {
throw NitroSQLiteError.fromError(error)
}
},
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Resource Leaks from Failed Close Blocking Reopen

Resource leak in error handling: If HybridNitroSQLite.close() throws an error, closeDatabaseQueue() is never called, leaving the database queue entry in the databaseQueues map. This prevents the database from being reopened later since openDatabaseQueue() will throw an error saying the database is already open. The code should ensure closeDatabaseQueue() is called even if closing the native database fails, possibly by reversing the order or using a try-finally pattern.

Fix in Cursor Fix in Web

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

@chrispader chrispader merged commit 5a7be1c into main Nov 6, 2025
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants